package ks.client.controllers.game;

import ks.client.UserContext;
import ks.client.ipc.Client;
import ks.client.parser.CommandParser;
import ks.framework.common.Message;

import org.w3c.dom.Document;

/**
 * Attempt to process keyboard commands from the lobby text field.
 * 
 * @author szutshi
 */
public class ProcessTableInputController {
	
	/** Context governing communication. */
	UserContext context;
	int num;
	
	/**
	 * The Table Input processor needs to know the UserContext to complete
	 * its tasks.
	 * 
	 * @param context
	 */
	public ProcessTableInputController (UserContext context, int num) {
		this.context = context;
		this.num = num;
	}
	
	/**
	 * Process string entered by user.
	 *  
	 * @param s
	 */
	public void process(String s) {
		// process commands from keyboard.
		Message m = CommandParser.process(s);
		Client client = context.getClient();
		System.out.println("YOU ARE IN THE TABLE INPUT CHAT CONTROLLER!");
		if (client == null) {
			System.err.println ("Not connected to server.");
			return;
		}
		
		if (m != null) {
			// all done.
			client.sendToServer(m);
			return;
		}
		// must have been a chat command all along. 
		StringBuilder cmd = new StringBuilder("<command version='1.0' id='589a39591271844e3fbe32bbb9281ad9'>\n");
		cmd.append("<table-chat table=\"" + num + "\">");
		cmd.append("<text>" + s + "</text>");
		cmd.append("</table-chat>");
		cmd.append("</command>");
		Document d = Message.construct(cmd.toString());
		m = new Message(d);
		client.sendToServer(m);
	}
}
